package com.lancoo.klgcourseware.ui.activity;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;

import androidx.fragment.app.FragmentTransaction;

import com.lancoo.klgcourseware.R;
import com.lancoo.klgcourseware.base.BaseKlgActivity;
import com.lancoo.klgcourseware.entity.bean.KlgConstant;
import com.lancoo.klgcourseware.ui.fragment.klgSubjectFragment.KnowledgeEmptyFragment;
import com.lancoo.klgcourseware.ui.fragment.klgSubjectFragment.KnowledgeEnglishFragment;
import com.lancoo.klgcourseware.ui.fragment.klgSubjectFragment.SubjectKnowledgeFragment;
import com.lancoo.klgcourseware.utils.KlgUtils;

import java.io.File;

/**
 * Created by pengmi on 2019/8/14 14:48
 * Description:
 */
public class KlgCourseWareMainActivity extends BaseKlgActivity {

    public static final String KEY_KLGCODE="klgCode";
    public static final String KEY_KLGTEXT="klgText";
    public static final String KEY_EXPANDTRAIN="expandTrain";
    private long startTime;


    @Override
    protected int getContentViewId() {
        return R.layout.klg_activity_courseware_main;
    }

    @Override
    protected String getTitleName() {
        return null;
    }

    @Override
    protected void setViewAndUpdata() {
        tv_title.setTextSize(18);
        startTime=System.currentTimeMillis();
        enterKlgStudyByKlgCode();
    }

    public static void launch(Activity mContext, String klgCode, boolean expandTrain){
        Intent mIntent=new Intent(mContext,KlgCourseWareMainActivity.class);
        mIntent.putExtra(KEY_KLGCODE,klgCode);
//        mIntent.putExtra(KEY_KLGCODE,"CE060300ζA015FBAA");
        mIntent.putExtra(KEY_EXPANDTRAIN,expandTrain);
        mIntent.putExtra(KEY_KLGTEXT,"");
        mContext.startActivityForResult(mIntent,KlgConstant.KLG_REQUESTCODE);
    }
    public static void launch(Context mContext, String klgCode, boolean expandTrain){
        Intent mIntent=new Intent(mContext,KlgCourseWareMainActivity.class);
        mIntent.putExtra(KEY_KLGCODE,klgCode);
        mIntent.putExtra(KEY_KLGTEXT,"");
//        mIntent.putExtra(KEY_KLGCODE,"CE060300ζA015FBAA");
        mIntent.putExtra(KEY_EXPANDTRAIN,expandTrain);
        mContext.startActivity(mIntent);
    }
    public static void launch(Context mContext, String klgCode,String klgText, boolean expandTrain){
        Intent mIntent=new Intent(mContext,KlgCourseWareMainActivity.class);
        mIntent.putExtra(KEY_KLGCODE,klgCode);
        mIntent.putExtra(KEY_KLGTEXT,klgText);
//        mIntent.putExtra(KEY_KLGCODE,"CE060300ζA015FBAA");
        mIntent.putExtra(KEY_EXPANDTRAIN,expandTrain);

        mContext.startActivity(mIntent);
    }


    private String[] klgArray={"CD050180fA00fBCAA","CD050180vA00vCBAA","CD050180EA00EDAAA",
            "CD050180XA00XECAA","CD050180DA00DFAAA","CD050180dA00dHDAA"};
    private static int index=0;
    private void enterKlgStudyByKlgCode(){
        FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
        String klgCode=getIntent().getStringExtra(KEY_KLGCODE);
//        KlgDbConstant.klg_baseUrl="http://122.112.252.195:10106/webapi_aisearch/";
//        klgCode="CC040274sA06QCCAA";
//        klgCode=klgArray[index];
//        index++;
//        if(index==klgArray.length){
//            index=0;
//        }
//        klgCode="CV0100000A1Rz00G0";
        SubjectKnowledgeFragment fragment;
        if(TextUtils.isEmpty(klgCode)){
            klgCode="null";
            fragment = new KnowledgeEmptyFragment();
        }else{
            switch (klgCode.substring(0,1)){
                case "C":
                case "c":
                    fragment =new KnowledgeEnglishFragment();
                    break;
                default:
                    setKlgName("");
                    fragment = new KnowledgeEmptyFragment();
            }
        }
        Log.e("pmpmpm","klgCode:"+klgCode);
        Bundle mBundle=new Bundle();
        mBundle.putString(KEY_KLGCODE,klgCode);
        mBundle.putBoolean(KEY_EXPANDTRAIN,getIntent().getBooleanExtra(KEY_EXPANDTRAIN,true));
        fragment.setArguments(mBundle);
        ft.replace(R.id.framelayout, fragment,klgCode);
        ft.commitAllowingStateLoss();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(isFinishing()){
            clearKlgTempFile();
        }
    }


    public void setKlgName(String name){
        Log.e("aaaa","setKlgName:"+getIntent().getStringExtra(KEY_KLGTEXT));
        if(tv_title!=null){
            if(TextUtils.isEmpty(name)){
                if(!TextUtils.isEmpty(getIntent().getStringExtra(KEY_KLGTEXT))){
                    tv_title.setText(getIntent().getStringExtra(KEY_KLGTEXT));
                }

            }else{
                tv_title.setText(name);
            }
        }
    }

    private void clearKlgTempFile(){
        File mRecordTempDir=new File(KlgUtils.abainRecordDir(this).getAbsolutePath(),"klgtemp");
        if(mRecordTempDir.exists()){
            File[] tempRecordFiles = mRecordTempDir.listFiles();
            for (File file : tempRecordFiles) {
                if (file.exists()) {
                    file.delete();
                }
            }
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId()==android.R.id.home){
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        Intent mIntent=new Intent();
        int study=0;
        if(startTime!=0){
            study= (int) ((System.currentTimeMillis()-startTime)/1000);
        }
        mIntent.putExtra(KlgConstant.KEY_KLG_STUDYTIME,study);
        Log.e("eeeeeee","学习时长："+study);
        setResult(KlgConstant.KLG_RESULTCODE,mIntent);
        super.onBackPressed();
    }
}
